<template>
  <view>
    <my-search sticky
               focus
               @input="searchInput"></my-search>

    <!-- 搜索结果列表 -->
    <view class="search-list-box"
          v-show="searchResultList.length">
      <navigator class="search-item"
                 v-for="item in searchResultList"
                 :key="item.goods_id"
                 :url="`/subpack/good_detail/good_detail?good_id=${item.goods_id}`">
        <view class="search-title">{{item.goods_name}}</view>
        <uni-icons type="arrowright"></uni-icons>
      </navigator>
    </view>
    <view class="history-box"
          v-show="!searchResultList.length">
      <view class="history-title-box">
        <view class="history-title">搜索历史</view>
        <uni-icons class="history-delete"
                   type="trash"
                   @click="historyDeleteClick"></uni-icons>
      </view>
      <view class="history-body-box">
        <uni-tag v-for="item in historyList"
                 :key="item"
                 :text="item"
                 :circle="true"
                 @click="historyItemClick(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        // 搜索结果
        searchResultList: [],
        // 历史数据
        historyList: []
      };
    },
    mounted() {
      let history = uni.getStorageSync('history') || '[]'
      this.historyList = JSON.parse(history)
    },
    beforeDestroy() {
      clearTimeout(this.timer)
    },
    methods: {
      /**
       * 搜索框输入事件
       */
      searchInput(value) {
        // 防抖，清除定时器
        clearTimeout(this.timer)
        // 添加新的定时器
        this.timer = setTimeout(() => {
          this.getSearchResultList(value.trim())
        }, 500)
      },
      /**
       * 历史数据删除点击事件
       */
      historyDeleteClick() {
        uni.removeStorageSync('history')
        this.historyList = []
      },
      /**
       * 历史数据项点击事件
       */
      historyItemClick(value) {
        uni.navigateTo({
          url: `/subpack/good_list/good_list?query=${value}`
        })
      },
      /**
       * 关键词搜索
       * @param {String} value 关键字
       */
      getSearchResultList(value) {
        // 关键词为空，清空搜索结果，不执行搜索
        if (!value || value.length === 0) {
          this.searchResultList = []
          return
        }
        this.addHistoryList(value)
        return uni.$http.get('goods/search', {
          query: value
        }).then(res => {
          let data = res.data
          if (data.meta.status === 200) {
            this.searchResultList = data.message.goods
            if (data.message.total === 0)
              uni.$showError('没有搜索结果')
          } else
            uni.$showError()
        })
      },
      /**
       * 将搜索关键字添加到历史记录
       * @param {String} value 搜索关键字
       */
      addHistoryList(value) {
        let index = this.historyList.indexOf(value)
        if (index > -1)
          delete this.historyList[index]
        this.historyList = [value, ...this.historyList]
        uni.setStorageSync('history', JSON.stringify(this.historyList))
      }
    }
  }
</script>

<style lang="scss">
  page {
    font-size: 28rpx;
  }

  .search-item {
    padding: 24rpx 10rpx;
    border-bottom: 1px solid #efefef;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .search-title {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }

  .history-box {
    padding: 0 14rpx;

    .history-title-box {
      border-bottom: 1px solid #efefef;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .history-title {
        line-height: 60rpx;
      }
    }

    .history-body-box {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 20rpx;
        margin-left: 20rpx;
      }
    }
  }
</style>
